-- BoxPreView  宝箱剩余奖励次数不足时开启的界面

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.BoxScene

local RARE_TIPS1 = {"restrictedreward4_1_surface", "restrictedreward5_1_surface", "restrictedreward6_1_surface"}

function ClassRef:show()
    self:setVisible(true)
    self:openAction()
    self:performWithDelay(function()
        self:playAction("animation", true)
    end, 1)
end

-- data = {boxType=, id=}
function ClassRef:init()
    self._ui.Button_quxiao:setVisible( false )
    self._ui.Button_queding:setVisible( false )
    self._ui.tips_0:setVisible(false)
    self._ui.tips_1:setVisible( false )
    self._ui.tips_2:setVisible( false )
    self._ui.tips_img_2:setVisible(false)
    self._ui.tips_3:setVisible( true )

    if self._data.boxType == Enums.BoxType.dungeon
        or self._data.boxType == Enums.BoxType.pvp
        or self._data.boxType == Enums.BoxType.eventBox then -- 地城/竞技/活动宝箱
        self._ui.tips_0:setVisible(true)
        self._ui.tips_0:setString(L(RARE_TIPS1[self._data.boxType]))
    end
    self._textBgSize = self._ui.tips_3:getContentSize()

    self._ui.Button_close:setVisible( true )
    self._ui.Button_close:addClickEventListener(function()
            self:closeView()
        end)

    local icon = self._ui["Panel/icon_1/icon"]
    local path = Res.BoxIconAry[self._data.boxType]
    if path then
        icon:loadTexture(path)
    end

    self:_handleUiVisible()
end

-- 根据类型处理UI可见性
function ClassRef:_handleUiVisible()
    local titleStr
    if self._data.boxType == Enums.BoxType.dungeon then
        titleStr = L("text_11900006")
    elseif self._data.boxType == Enums.BoxType.secret then
    elseif self._data.boxType == Enums.BoxType.pvp then
        titleStr = L("text_11900007")
    elseif self._data.boxType == Enums.BoxType.eventBox then -- 世界活动副本宝箱
        titleStr = L("text_11900008")
    elseif self._data.boxType == Enums.BoxType.guildChallenge then -- 公会挑战
        titleStr = L("lua_code_text_20")

        self._ui.bg3:setVisible( false )
        self._ui.icon:setVisible( false )
    elseif self._data.boxType == Enums.BoxType.champion then -- 冠军赛挑战
        titleStr = L("lua_code_text_92")

        self._ui.bg3:setVisible( false )
        self._ui.icon:setVisible( false )
    end

    titleStr = titleStr or L("lua_code_text_326")

    self._ui.tlt:setString( titleStr )

    -- 地城/竞技/活动宝箱的tips显示
    local weeklyTimes = UD:getLuckBoxWeeklyRestTimesByType(self._data.boxType)
    local dayTimes = UD:getLuckBoxRestTimesByType(self._data.boxType)
    local textStr, maxNum
    if weeklyTimes <= 0 then
        maxNum = UD:getLuckBoxWeeklyMaxNum(self._data.boxType)
        textStr = L("text_11900009")
    elseif dayTimes <= 0 then
        maxNum = UD:getLuckBoxDailyMaxNum(self._data.boxType)
        textStr = L("text_11900005")
    end
    local text = self._ui.tips_3:getChildByName("text")
    text:setString( string.format( textStr, maxNum, titleStr ) )

    local textSize = text:getVirtualRendererSize()
    if textSize.width > (self._textBgSize.width - 12) then
        self._textBgSize.width = textSize.width + 12
        self._ui.tips_3:setContentSize(self._textBgSize)
        ccui.Helper:doLayout(self._ui.tips_3)
    end
end

return ClassRef
